//
//  ZDWalletCell.m
//  特种车调度
//
//  Created by 陈宇 on 15/8/19.
//
//

#import "ZDWalletCell.h"
#import "ZDWalletDetailCell.h"
#import "ZDWalletLog.h"
#import "BMAccountManager.h"

@interface ZDWalletCell () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *pirceLabel;
@property (weak, nonatomic) IBOutlet UILabel *subTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeightConstraint;

@end

@implementation ZDWalletCell

- (void)awakeFromNib {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDWalletDetailCell" bundle:nil] forCellReuseIdentifier:reuseIdentifier];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _log.serviceLogs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDWalletDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    cell.log = _log.serviceLogs[indexPath.row];
    return cell;
}

- (void)setLog:(ZDWalletLog *)log
{
    _log = log;
    
    _titleLabel.text = _log.title;
    _subTitleLabel.text = _log.subTitle;
    if ([_log.pay isEqualToString:[BMAccountManager sharedInstance].user.disId]) {
        _pirceLabel.text = [NSString stringWithFormat:@"-%.2f", _log.realsumPrice > _log.money ? _log.realsumPrice : _log.money];
    } else {
        _pirceLabel.text = [NSString stringWithFormat:@"+%.2f", _log.realsumPrice > _log.money ? _log.realsumPrice : _log.money];
    }
    _timeLabel.text = _log.operateDate;
    
    if (_log.selected) {
        [self.tableView reloadData];
        _tableViewHeightConstraint.constant = _tableView.contentSize.height;
    } else {
        _tableViewHeightConstraint.constant = 0;
    }
}

@end
